import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: true,
		userInfo: uni.getStorageSync('riderUserInfo'),
	},
	mutations: {
		// 使vuex不会因刷新而掉
		persistenceVuex(state, userInfo){
			if(uni.getStorageSync('riderToken')){
				state.hasLogin = true;
			}
			state.userInfo = userInfo;
			uni.removeStorage({
				key: 'riderUserInfo'
			})
			uni.setStorageSync('riderUserInfo', userInfo); //缓存用户登陆状态
		},
		// 更新用户信息
		updateUserInfo(state, userInfo) {
			state.userInfo = userInfo;
			uni.removeStorage({
				key: 'riderUserInfo'
			})
			uni.setStorageSync('riderUserInfo', userInfo); //缓存用户登陆状态
		},
		login(state, provider) {
			state.hasLogin = true;
			state.userInfo = provider;
			uni.setStorageSync('riderUserInfo', provider); //缓存用户登陆状态
			uni.setStorageSync('riderToken', provider.token);
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			uni.removeStorage({
				key: 'riderUserInfo'
			})
			uni.removeStorage({
				key: 'riderToken'
			})
		}
	},
	actions: {

	}
})

export default store